package com.clgk.wechatmall.common.util;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

/**
 * 获取修正后的点
 * 
 * @author tcloud
 *
 */
public class MapPointFix
{
    private static Logger logger = Logger.getLogger(MapPointFix.class);

    /**
     * 点的偏差集合
     */
    private static Map<String, BigDecimal[]> pDeviation = new ConcurrentHashMap<String, BigDecimal[]>();

    // public static void main(String[] args) throws Exception
    // {
    // // 22.55562400727999, lng:113.94879631710046
    // new MapPointFix().init();
    // System.out.println(org.springframework.util.StringUtils
    // .arrayToCommaDelimitedString(getFixedPoint(
    // "122.22", "23.55", true)));
    // //113.704421, 34.749438
    // //113.710581,34.748338
    //
    // //113.697853,34.750771
    // //113.703883,34.749521
    //
    //
    // //
    // System.out.println(getDistance(113.698261,34.750538,113.697853,34.750771));
    // //
    // System.out.println(getDistance(113.710581,34.748338,113.703883,34.749521));
    //
    // }

    /**
     * 读取偏差表，计算偏差
     * 
     * @throws Exception
     */
    public void init() throws Exception
    {
        logger.info("read point deviation begin......");
        long beginTime = System.currentTimeMillis();
        // List<String> points = FileUtils.readLines(new File("C:/table.txt"));
        List<String> points = IOUtils.readLines(
        		MapPointFix.class.getClassLoader().getResourceAsStream("table.txt"));
        int x = 719;
        int y = 99;
        // x轴标准点值
        BigDecimal pointXStandard = null;
        // y轴标准点值
        BigDecimal pointYStandard = null;
        // 每个点与标准点的偏差
        BigDecimal[] pd = null;
        for (String pointStr : points)
        {
            // 每个pointXY有两个点
            String[] pointXY = StringUtils.trim(pointStr).split("\\s+");
            x += 1;
            if ("7200000".equals(pointXY[0]))
            {
                x = 720;
                y += 1;
                pointYStandard = new BigDecimal(y).divide(new BigDecimal(10));
            }

            // 计算第一个点的偏差
            pointXStandard = new BigDecimal(x).divide(new BigDecimal(10));
            pd = new BigDecimal[]
            {
                    new BigDecimal(pointXY[0]).divide(new BigDecimal(100000))
                            .subtract(pointXStandard),
                    new BigDecimal(pointXY[1]).divide(new BigDecimal(100000))
                            .subtract(pointYStandard) };
            pDeviation.put(String.valueOf(x) + y, pd);
            // 计算第二个点的偏差
            x += 1;
            pointXStandard = new BigDecimal(x).divide(new BigDecimal(10));
            pd = new BigDecimal[]
            {
                    new BigDecimal(pointXY[2]).divide(new BigDecimal(100000))
                            .subtract(pointXStandard),
                    new BigDecimal(pointXY[3]).divide(new BigDecimal(100000))
                            .subtract(pointYStandard) };
            pDeviation.put(String.valueOf(x) + y, pd);
        }
        logger.info("read point deviation end...... "
                + (System.currentTimeMillis() - beginTime) + "ms");
    }

    /**
     * 获取修正后的点
     * 
     * @param longitude
     * @param latitude
     * @param toGMap
     * @return
     */
    public static double[] getFixedPoint(double lng, double lat, boolean toGMap)
    {
        String longitude = String.valueOf(lng);
        String latitude = String.valueOf(lat);
        int i = longitude.indexOf(".");
        int j = latitude.indexOf(".");
        // 点的偏差集合中的key
        String pDeviationKey = longitude.substring(0, i)
                + longitude.substring(i + 1, i + 2) + latitude.substring(0, j)
                + latitude.substring(j + 1, j + 2);
        BigDecimal[] pd = pDeviation.get(pDeviationKey);
        double[] point = new double[]
        { lng, lat };
        if (pd != null)
        {
            if (toGMap)
            {
                point = new double[]
                { new BigDecimal(longitude).add(pd[0]).doubleValue(),
                        new BigDecimal(latitude).add(pd[1]).doubleValue() };
            }
            else
            {
                point = new double[]
                { new BigDecimal(longitude).subtract(pd[0]).doubleValue(),
                        new BigDecimal(latitude).subtract(pd[1]).doubleValue() };
            }
        }
        return point;
    }
}
